﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace YKXiangQi.ViewModels
{
    [Serializable]
    public class ViewModelBase: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            var memberExpression = propertyExpression.Body as MemberExpression;
            if (memberExpression != null)
                RaisePropertyChanged(memberExpression.Member.Name);
        }

        public virtual void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private bool _isDirty;
        public bool IsDirty
        {
            get { return _isDirty; }
            private set
            {
                _isDirty = value;
                RaisePropertyChanged(() => this.IsDirty);
            }
        }

        public virtual void UserChanged()
        {
            IsDirty = true;
        }

        public virtual void UserCommitted()
        {
            IsDirty = false;
        }
    }
}
